<?php
namespace app\index\model;
use think\Model;

class RoomQuestion extends Model
{
    
    public function getOne ($where) {
        $res = $this->where($where)->find();
        if($res) {
            return $res->toArray();
        }
        return false;
    }
    
    public function getList ($where) {
        $res = $this->where($where)->select();
        if($res) {
            return $res->toArray();
        }
        return false;
    }
    
    public function getShuffleData ($data,$roomid) {
        //打乱顺序
        if(gettype($data)=='object') {
            $data = $data->toArray();
        }
        $list = [
            0 => [
                'key' => 1,
                'value' => $data['answer1']
            ],
            1 =>  [
                'key' => 2,
                'value' => $data['answer2']
            ],
            2 =>  [
                'key' => 3,
                'value' => $data['answer3']
            ],
            3 =>  [
                'key' => 4,
                'value' => $data['answer4']
            ]
        ];
        shuffle($list);
        $new = $data;
        foreach ($list as $k=>$v) {
            $new["answer".($k+1)] = $v['value'];
            if($v['key']==$data['answer_right']) {
                $new['answer_right'] = $k+1;
            }
        }
        $new['room_id'] = $roomid;
        $new['create_time'] = time();
        return $new;
    }
    
    public function getAllScore ($uid,$roomId) {
        $where = [
            'uid' => $uid,
            'room_id' => $roomId
        ];
        return $this->where($where)->sum('score');
    }
    
    public function getRightNumber ($uid,$roomId) {
        $where = [
            'uid' => $uid,
            'room_id' => $roomId,
            'status' => 1
        ];
        return $this->where($where)->count();
    }
}
